package com.wan.core.strategy.selector;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

public final class StrategyRandomSelector<T> implements Selector<Void, T> {

    private final List<T> objs;
    private final Random random;

    public StrategyRandomSelector(Collection<T> objs) {
        this.objs = new CopyOnWriteArrayList<>(objs);
        random = new Random(99);
    }

    @Override
    public Optional<T> get(Void k) {
        T t = objs.get(random.nextInt(objs.size()));
        return Optional.of(t);
    }

    @Override
    public List<T> getAll() {
        return objs;
    }

    @Override
    public void add(Void k, T t) {
        objs.add(t);
    }

    @Override
    public void remove(Void k, T t) {
        objs.remove(t);
    }
}
